package ru.java2D;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Admin
 * Date: 14.04.13
 * Time: 19:47
 * To change this template use File | Settings | File Templates.
 */
public class Player {

    int v = 0;
    int dv = 0;
    int s = 0;

    int layer1=0;
    int layer2=1100;

    int x = 20;
    int y = 150;
    int length = 190;
    int width = 100;
    int dy = 0;

    public static final int MAX_V = 40;
    public static final int MAX_TOP = 10;
    public static final int MAX_BOTTOM = 370;

    Image img_c = new ImageIcon(getClass().getClassLoader().getResource("res/t34.jpg")).getImage();
    Image img_l = new ImageIcon(getClass().getClassLoader().getResource("res/t34l.jpg")).getImage();
    Image img_r = new ImageIcon(getClass().getClassLoader().getResource("res/t34r.jpg")).getImage();

    Image img = img_c;

    public Rectangle getRect(){
        return new Rectangle(x, y, length, width);
    }

    public void move(){

        s += v;

        v += dv;
        if (v <= 0) {v = 0;}
        if (v >= MAX_V) {v = MAX_V;}

        y -= dy;
        if (y <= MAX_TOP) {y = MAX_TOP;}
        if (y >= MAX_BOTTOM) {y = MAX_BOTTOM;}

        if (layer2 - v <= 0){
            layer1=0;
            layer2=1100;
        }
        else {
            layer1 -= v;
            layer2 -= v;
        }
    }

    public void keyPressed (KeyEvent e){
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_RIGHT){
            dv = 1;
        }
        if (key == KeyEvent.VK_LEFT){
            dv = -1;
        }
        if (key == KeyEvent.VK_UP && v != 0){
            dy = 10;
            img = img_l;
        }
        if (key == KeyEvent.VK_DOWN && v != 0){
            dy = -10;
            img = img_r;
        }
    }

    public void keyReleased (KeyEvent e){
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_LEFT){
            dv = 0;
        }
        if (key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN){
            dy = 0;
            img = img_c;
        }
    }
}
